# -*- coding: utf-8 -*-
import subprocess
import os
import moviepy.editor as mp

url = "https://www.bilibili.com/video/BV15B4y1G7F3/"

video_link = url
print(video_link)

p = subprocess.run(["you-get", video_link],
                   capture_output=True,
                   encoding="utf-8", timeout=20)

res = p.stdout.splitlines()
print(res)
title = (res[1].split(":")[1]).strip()
# title = "一千個傷心的理由"
# title = res[1]
# print(res[3])
print(title)
format = (res[3].split(":")[1]).strip()
# format = "flv"
print(format)
fileName = title.split(".")[0] + '.' + format
print(fileName)

my_clip = mp.VideoFileClip(filename=fileName)
# my_clip.audio.write_audiofile(title + ".mp3")
# my_clip.audio.write_audiofile("一千個傷心的理由" + ".mp3")
my_clip.audio.write_audiofile("Love Story" + ".mp3")
my_clip.close()

os.remove(fileName)
os.remove(title + ".cmt.xml")
